﻿using System;
using System.Collections.Generic;

using TempEmployees.BLL.UserAccess.ActiveDirectory.Configuration;

namespace TempEmployees.BLL.UserAccess.ActiveDirectory
{
    public static class ActiveDirectoryValueConversions
    {
        private static Dictionary<string, int> m_roles = new Dictionary<string, int>();
        private static Dictionary<string, int> m_departments = new Dictionary<string, int>();

        private static int m_employeeRoleId;

        static ActiveDirectoryValueConversions()
        {
            ReadConfigurations();
        }

        private static void ReadConfigurations()
        {
            ActiveDirectorySection eeActiveDirectoryPluginConfiguration =
            System.Configuration.ConfigurationManager.GetSection("ActiveDirectoryConfiguration") as ActiveDirectorySection;

            RolesCollectionElement roles = eeActiveDirectoryPluginConfiguration.Roles;
            RolesCollectionElement departments = eeActiveDirectoryPluginConfiguration.Departments;

            foreach (RoleElement role in roles)
            {
                m_roles.Add(role.DN, role.ID);
            }

            foreach (RoleElement department in departments)
            {
                m_departments.Add(department.DN, department.ID);
            }

            m_employeeRoleId = eeActiveDirectoryPluginConfiguration.EmployeeRoleID;
        }

        public static int ToRoleID(string dn, ref int departmentId)
        {
            foreach (string department in m_departments.Keys) 
            {
                if (department != dn) continue;
                departmentId = m_departments[department];
                return m_employeeRoleId;
            }
            foreach (string role in m_roles.Keys)
            {
                if (role == dn) return m_roles[role];
            }
            return 0;
        }

        public static int ToDepartmentID(string dn)
        {
            return m_departments[dn];
        }
    }
}